/*
 *	profile-view view
 *  -----------------------------------------------------------------------------------------------------------
 */

site.view('profile-view', {
	
	onLoad: function(settings) {
		this.startAll(settings);
	},
	
	onUnload: function() {
		this.stopAll();
	}

})

/*
 *	contact-scroll
 *  -----------------------------------------------------------------------------------------------------------
 */

.module('contact-scroll', function(context) {
	var contact_list = context.query('#mini_contact_list > ul'),
		slim_scroll = context.require('slimScroll');

	return {
		init: function() {
			if(contact_list.children('li').size() > 7) {
				slim_scroll.init(contact_list, { 
					height: '329px', 
					right: '0px', 
					'z-index': 1 
				})	
			}
			
		}
	}
})

/*
 *	toggle-message-box
 *  -----------------------------------------------------------------------------------------------------------
 */

.module('toggle-message-box', function(context) {
	var toggle_box = context.require('toggleBox');

	return {
		init: function() {
			toggle_box.init({
				anchor: '#message_icon_mini',
				box: '#message_box',
				onToggle: function() {
					//cleare old error style input and reset input values
					context.query('#message_box form')[0].reset();
					context.query('#message_box form #subject, #message_box form #content_message').removeAttr('style');
				}
			});	
		},

		events: {
			'window.click': function() { 
				var temp_anchor = context.query('#message_icon_mini');
				if(temp_anchor.hasClass('active')) {
					temp_anchor.removeClass('active');
					context.query('#message_box').hide();
   				}
			}
		}
	}
})

/*
 *	send-message-form
 *  -----------------------------------------------------------------------------------------------------------
 */

.module('send-message-form', function(context) {
	var request = context.request,
		form = $('#message_box form'),
		notification_container = $("#notification_container"),
		form_on_progres = 0,
		error_message,
		error_input_style = { 'border-color': '#c00000' },
		has = 'hasOwnProperty';

	return {
		init: function() {

			if (form_on_progres == 0) {
				form_on_progres = 1;

				form.on('submit', function(e) {					

					request({
						url: form.attr('action'),
	                    type: 'post',
	                    dataType: 'json',
	                    data: form.serialize({checkboxesAsBools: true}),
	                    success: function(data)
	                    {
	                    	//clear error_message and error element styles 
							error_message = '';
							form.find('#subject, #content_message').removeAttr('style');

	                    	if(data.success == 'true')
	                        {
	                              notification_container.notify("create", {
		                              title: data.message,
		                              text: data.description
	                              });  
	                              
	                              form[0].reset();                                                           
	                        }
	                        else
	                        {
	                        	

	                        	//prepare error message
	                        	for (var message in data.errors) {
	                        		if(data.errors[has](message)) {
	                        			$('#' + message).animate(error_input_style);
	                        			error_message += data.errors[message] + '<br/>';
	                        		}
	                        	}	
	                        	

	                        	notification_container.notify('create', 'error-template', {
	                                 title: data.message,
	                                 text: error_message
	                            }, {
	                                custom:true
	                                //expires: false
	                            });   
	                        }

	                        form_on_progres = 0;
	                    }

					});
					e.preventDefault();
				})
			}
			
		}
	}
})


/*
 *	add-user-to-contact
 *  -----------------------------------------------------------------------------------------------------------
 */

.module('add-user-to-contact', function(context) {
	var action_dialog = context.require('actionDialog'),
		notify = context.require('notify'),
		request = context.request;
	
	return {
		init: function() {
		
			action_dialog.create('#friend_icon_mini', function(anchor) {
	        	request({
					url: anchor.attr('href'),
					type: 'post',
					dataType: 'json',
					success: function(data) {

						notify.create('create', {
									title: data.message,
									text:  data.description
								});

						anchor.parent('li').slideUp();
					}
				});
			});
		}
	}
})